new
    CQS_FileDataKeys[18][13]=
    {
		"CQS_point1",
		"CQS_point2",
		"CQS_point3",
		"CQS_point4",
		"CQS_name1",
		"CQS_name2",
		"CQS_name3",
		"CQS_name4",
		"CQS_CPOfSet1",
		"CQS_CPOfSet2",
		"CQS_CPOfSet3",
		"CQS_CPOfSet4",
		"CQS_owner1",
		"CQS_owner2",
		"CQS_owner3",
		"CQS_owner4",
		"CQS_msu1",
		"CQS_msu2"
	},
	Text3D:CPlabel	[MAX_CP][2],
	CPicon			[MAX_CP][2],

	CPowner			[MAX_CP]=-1,
	CPname          [MAX_CP][MAX_CPNAME],
	Float:CPpoint	[MAX_CP][3],
	Float:CPOfSet   [MAX_CP][3],
	CPflag			[MAX_CP],
	Bar:CPbar		[MAX_CP],
	CPfill			[MAX_CP],
	bool:CPtimer	[MAX_CP],
	FirstToCapture  [MAX_CP],
	CPFlash         [MAX_CP]=-1,
	CapturingCP		[MAX_PLAYERS]=-1;


//======================Conquest Functions

	stock GetPlayersCapturing(cp)
	{
		new c;
		PlayerLoop(i)if(CapturingCP[i]==cp)c++;
		return c;
	}
	stock GetPlayersInTeamCapturing(teamid, cp)
	{
		new c;
		PlayerLoop(i) if( (pTeam(i)==teamid) && (IsPlayerInDynamicArea(i, CPflag[cp])) ) c++;
		return c;
	}
	stock GetRavenCPs()
	{
		new TcpS;
		for(new t;t<MAX_CP;t++)if(CPowner[t] == 0) TcpS++;
		return TcpS;
	}
	stock GetValorCPs()
	{
		new TcpS;
		for(new t;t<MAX_CP;t++)if(CPowner[t] == 1) TcpS++;
		return TcpS;
	}
	stock GetEmptyCPs()
	{
		new TcpS;
		for(new t;t<MAX_CP;t++)if(CPowner[t] == -1) TcpS++;
		return TcpS;
	}


CP_CaptureUpdate(playerid, cp)
{
	new
		friendlyCaptureCount	= GetPlayersInTeamCapturing(pTeam(playerid), cp),
		enemyCaptureCount		= GetPlayersInTeamCapturing(pTeam(playerid), cp);

	if(enemyCaptureCount>friendlyCaptureCount)CPFlash[cp]=1;
	else
	{
	    CPFlash[cp]=-1;
		if(CPowner[cp] == -1)
		{
			SetProgressBarColor(CPbar[cp], BLUE);
			CPfill[cp] += (1*GetPlayersCapturing(cp));
			SetProgressBarValue(CPbar[cp], CPfill[cp]);
			PlayerLoop(i)if(CapturingCP[i]==cp)UpdateProgressBar(CPbar[cp], i);
			if(CPfill[cp]==20)
			{
				OnCPCapture(playerid, cp);
				CPfill[cp]=0;
				CPtimer[cp]=false;
			}
		}
		if(CPowner[cp] == poTeam(playerid))
		{
		    SetProgressBarColor(CPbar[cp], RED);
			CPfill[cp] -= (1*GetPlayersCapturing(cp));
			SetProgressBarValue(CPbar[cp], CPfill[cp]);
			PlayerLoop(i)if(CapturingCP[i]==cp)UpdateProgressBar(CPbar[cp], i);
			if(CPfill[cp]==0)
			{
			    OnCPLoss(playerid, cp);
				CPfill[cp]=0;
			}
		}
		if(CPowner[cp] == pTeam(playerid))
		{
		    msgF(playerid, YELLOW, "CP: "#C_ORANGE"%s"#C_YELLOW" Owned by your team", CPname[cp]);
			CPtimer[cp]=false;
		}
	}
	return 1;
}
OnCPLoss(playerid, cp)
{
	CPowner[cp]=-1;
	TeamMsgF(poTeam(playerid), BLUE, "We have lost %s", CPname[cp]);
	PlayerLoop(i)if(CapturingCP[i]==cp)GiveXP(i, 20, "Neutralized");
	SetCPTeam(cp, -1);
}
OnCPCapture(playerid, cp)
{
	TeamMsgF(pTeam(playerid), BLUE, "We Have Captured "#C_BLUE"%s!", CPname[cp]);
	TeamMsgF(poTeam(playerid), BLUE, "They Have Captured "#C_RED"%s!", CPname[cp]);
	HideProgressBarForPlayer(playerid, CPbar[cp]);
	PlayerLoop(i)
	{
		if(CapturingCP[i]==cp)
		{
			CapturingCP[i] = -1;
			GiveXP(i, 20, "Capture");
		}
	}
	SetCPTeam(cp, pTeam(playerid));
	CheckWinningState();
}

CheckWinningState()
{
	new TeamCPs[2];

	TeamCPs[0]=GetRavenCPs();
	TeamCPs[1]=GetValorCPs();

	for(new i;i<2;i++)
	{
		if(WinningTeam == -1)
		{
			if(TeamCPs[i] == MAX_CP)
			{
			    WinningTeam = i;
			    MatchTime(0, 20);
			    PlayerLoop(a)if(bPlayerGameSettings[a]&InDM)TextDrawShowForPlayer(a, MatchTimeCounter);
			}
		}
	}
	if(WinningTeam != -1)
	{
		if(TeamCPs[WinningTeam] < MAX_CP)
		{
			WinningTeam = -1;
			StopMatchTime();
		}
	}
}
SetCPTeam(cp, team)
{
	new tmpCP_Name[MAX_CPNAME+11];

	CPowner[cp] = team;

	PlayerLoop(i)if(bPlayerGameSettings[i]&InDM)
	{
		if(team==pTeam(i))
		{
			SetPlayerMapIcon(i, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, BLUE);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_BLUE"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
		if(team==poTeam(i))
		{
			SetPlayerMapIcon(i, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, RED);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_RED"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
		if(team==-1)
	    {
			SetPlayerMapIcon(i, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, WHITE);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_WHITE"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
	}
	return 1;
}
UpdateCPIconsForPlayer(playerid)
{
	new
	    tmpCP_Name[MAX_CPNAME+11],
		team = pTeam(playerid);
	for(new cp;cp<MAX_CP;cp++)
	{
		if(CPowner[cp]==team)
		{
			SetPlayerMapIcon(playerid, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, BLUE);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_BLUE"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
		if(CPowner[cp]!=team)
		{
			SetPlayerMapIcon(playerid, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, RED);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_RED"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
		if(CPowner[cp]==-1)
		{
			SetPlayerMapIcon(playerid, CPicon[cp][team], CPpoint[cp][0], CPpoint[cp][1], CPpoint[cp][2], 0, WHITE);
			format(tmpCP_Name, (MAX_CPNAME+11), ">> "#C_WHITE"%s"#C_YELLOW" <<", CPname[cp]);
			UpdateDynamic3DTextLabelText(CPlabel[cp][team], YELLOW, tmpCP_Name);
		}
    }
}

